package eu.dominicum.ft.pd;

import eu.dominicum.ft.pd.mao.MakeFantasyStageMAO;

import java.util.ArrayList;
import java.util.List;

/**
 * A Stage in a Fantasy Tour.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
public class FantasyStage extends PersistentDomainObject {

    /**
     * The sequence number.
     */
    private Integer sequence;

    /**
     * The associated {@link FantasyTour} object.
     */
    private FantasyTour fantasyTour;

    /**
     * The associated {@link Stage} object.
     */
    private Stage stage;

    /**
     * Protected constructor to satisfy JPA.
     */
    protected FantasyStage() {

    }

    /**
     * Makes a FantasyStage object.
     *
     * @param makeFantasyStageMAO -
     * @return FantasyStage
     */
    public static FantasyStage make(final MakeFantasyStageMAO makeFantasyStageMAO) {

        FantasyStage newFantasyStage = new FantasyStage();

        newFantasyStage.sequence = makeFantasyStageMAO.getStage().getSequence();
        newFantasyStage.stage = makeFantasyStageMAO.getStage();
        newFantasyStage.fantasyTour = makeFantasyStageMAO.getFantasyTour();

        return newFantasyStage;
    }

    /**
     * Calculate the scores for this FantasyStage object.
     *
     * @return List<FantasyScore>
     */
    public final List<FantasyScore> calculateScores() {

        List<FantasyScore> scores = new ArrayList<>();
        FantasyTourConfiguration fantasyTourConfiguration = this.fantasyTour.getConfiguration();
        for (StageResult stageResult : this.stage.getStageResults()) {
            for (StageResultDetail stageResultDetail : stageResult.getStageResultDetails()) {
                for (Participant participant : this.fantasyTour.getParticipants()) {
                    for (FantasyTeamMember fantasyTeamMember : participant.getFantasyTeamMembers()) {
                         if (fantasyTeamMember.getTeamMember().equals(stageResultDetail.getTeamMember())) {
                            scores.add(FantasyScore.calculate(fantasyTourConfiguration,
                                    fantasyTeamMember, participant, stageResultDetail, this));
                         }
                    }
                }
            }
        }
        return scores;
    }

    /**
     * .
     *
     * @return Integer
     */
    public final Integer getSequence() {
        return sequence;
    }

    /**
     * .
     *
     * @return FantasyTour
     */
    public final FantasyTour getFantasyTour() {
        return fantasyTour;
    }

    /**
     * .
     *
     * @return Stage
     */
    public final Stage getStage() {
        return stage;
    }

}
